#include "bsp_ntp.h"
#include "esp_sntp.h"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

/**
 * @func：转换为 GMT RF？？？格式的字符串
 */
String unixTimeToGMTString(time_t unixTime)
{
  char buffer[80];
  struct tm timeinfo;
  gmtime_r(&unixTime, &timeinfo);
  strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", &timeinfo);
  return String(buffer);
}



String getDateTime()
{
  // 请求网络时间
  timeClient.update();

  unsigned long epochTime = timeClient.getEpochTime();
  Serial.print("Epoch Time: ");
  Serial.println(epochTime);

  String timeString = unixTimeToGMTString(epochTime);

  // 打印结果
  Serial.println(timeString);
  return timeString;
}


// 处理url格式
String formatDateForURL(String dateString)
{      
  // 替换空格为 "+"
  dateString.replace(" ", "+");
  dateString.replace(",", "%2C");
  dateString.replace(":", "%3A");
  return dateString;
}


void setup_ntp_client()
{
  timeClient.begin();
  // 设置时区
  // GMT +1 = 3600
  // GMT +8 = 28800
  // GMT -1 = -3600
  // GMT 0 = 0
  timeClient.setTimeOffset(+28800);
}